namespace CoreManager.Models.DTOs
{
    /// <summary>
    /// 统一API响应格式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 数据
        /// </summary>
        public T? Data { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public long Timestamp { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

        /// <summary>
        /// 追踪ID，用于反查和日志追踪
        /// </summary>
        public string TraceId { get; set; } = string.Empty;

        /// <summary>
        /// 成功响应
        /// </summary>
        /// <param name="data"></param>
        /// <param name="message"></param>
        /// <param name="traceId"></param>
        /// <returns></returns>
        public static ApiResponse<T> Success(T data, string message = "操作成功", string traceId = "")
        {
            return new ApiResponse<T>
            {
                Code = 200,
                Message = message,
                Data = data,
                TraceId = traceId
            };
        }

        /// <summary>
        /// 失败响应
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <param name="traceId"></param>
        /// <returns></returns>
        public static ApiResponse<T> Fail(string message, int code = 500, string traceId = "")
        {
            return new ApiResponse<T>
            {
                Code = code,
                Message = message,
                TraceId = traceId
            };
        }
    }

    /// <summary>
    /// 分页响应
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PagedResponse<T>
    {
        /// <summary>
        /// 数据列表
        /// </summary>
        public List<T> Items { get; set; } = new();

        /// <summary>
        /// 总记录数
        /// </summary>
        public int Total { get; set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages => (int)Math.Ceiling((double)Total / PageSize);

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPrevious => PageIndex > 1;

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNext => PageIndex < TotalPages;
    }
}